package github.roki27.bckey.mixin;

import net.minecraft.Util;
import net.minecraft.client.KeyboardHandler;
import net.minecraft.client.Minecraft;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(KeyboardHandler.class)
public class KeyboardHandlerMixin {

    @Shadow
    private long debugCrashKeyTime;

    @Shadow
    @Final
    private Minecraft minecraft;

    @Inject(method = "keyPress(JIIII)V", at = @At("RETURN"))
    public void keyPress(long pWindowPointer, int pKey, int pScanCode, int pAction, int pModifiers, CallbackInfo ci) {
        if (pKey == 290) {
            this.minecraft.options.hideGui = false;
        }
    }

    @Inject(method = "handleDebugKeys(I)Z", at = @At("HEAD"), cancellable = true)
    private void handleDebugKeys(int pKey, CallbackInfoReturnable<Boolean> cir) {
        if (!(this.debugCrashKeyTime > 0L && this.debugCrashKeyTime < Util.getMillis() - 100L) && pKey == 66) {
            this.minecraft.getEntityRenderDispatcher().setRenderHitBoxes(false);
            cir.setReturnValue(true);
            cir.cancel();
        }
    }
}
